﻿using Common.Entity;
using Newtonsoft.Json;
using System;
using System.Net;
using System.Text;

namespace Common.Utility
{
    public class HttpHelper
    {
        public string PostRequest(string url, object jsonData)
        {
            try
            {

                string jsonreq = JsonConvert.SerializeObject(jsonData);


                using (WebClient client = new WebClient())
                {
                    byte[] data = Encoding.UTF8.GetBytes(jsonreq);

                    // 设置 Content-Type 头
                    client.Headers.Add(HttpRequestHeader.ContentType, "application/json");

                    // 执行 POST 请求，并获取响应
                    byte[] response = client.UploadData(url, "POST", data);

                    // 解码响应并显示结果
                    string result = Encoding.UTF8.GetString(response);

                    BeflamPostData beflamPost = JsonConvert.DeserializeObject<BeflamPostData>(result);
                    if (beflamPost.code != 0)
                    {
                        throw new Exception(beflamPost.message);
                    }
                    return result;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }


        //public string PostGetSession(string url, string uid, string pwd)
        //{
        //    try
        //    {
        //        string cookie_ck = "";

        //        url += $@"/Home/Login?txtuser={uid}&txtpwd={pwd}";
        //        HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        //        myHttpWebRequest.CookieContainer = new CookieContainer();
        //        myHttpWebRequest.CookieContainer.SetCookies(new Uri(url), "");
        //        HttpWebResponse myresponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
        //        return myresponse.Headers["Set-Cookie"];

        //        return cookie_ck;

        //    }
        //    catch (Exception ex)
        //    {

        //        throw ex;
        //    }
        //}
    }
}
